package com.lonelymeko.maven.aimodeltest.helloWorld;


import com.alibaba.fastjson2.*;
import com.lonelymeko.maven.aimodeltest.properties.AiApiProperties;
import com.lonelymeko.maven.aimodeltest.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * @author lonelymeko
 *
 */
@Component
public class DeepSeekChat {
    @Autowired
    AiApiProperties aiApiProperties;




    public String chat(String userContent, String systemContent) {

        Result result = getResult(userContent, systemContent);

        // 发送请求并解析响应
        try {
            return getString(result.paramMap(), result);

            // ... 省略不变更代码 ...
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public String chat(String userContent) {
        String systemContent = "你是一个AI助手，你的名字叫DeepSeek。";
        // 创建请求参数
        Result result = getResult(userContent, systemContent);


        // 发送请求并解析响应
        try {
            return getString(result.paramMap, result);

            // ... 省略不变更代码 ...
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    private String getString(Map<String, Object> result, Result result1) throws IOException {
        String response = HttpClientUtil.doPost4Json(aiApiProperties.getUrl(), result, result1.headers());
        // 检查响应是否为有效JSON

        JSONObject jsonResponse = JSON.parseObject(response);
//                System.out.println("原始响应: " + jsonResponse);
        JSONArray choices = jsonResponse.getJSONArray("choices");
        JSONObject object = (JSONObject) choices.get(0);
        String content = object.getJSONObject("message").getString("content");

//                System.out.println("响应: " + content);
        return content;
    }

    private Result getResult(String userContent, String systemContent) {
        // 创建请求参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("model", "deepseek-chat");
        // 将JSON字符串作为对象数组传入
        paramMap.put("messages", Arrays.asList(
                new HashMap<String, String>() {{
                    put("role", "system");
                    put("content", systemContent);
                }},
                new HashMap<String, String>() {{
                    put("role", "user");
                    put("content", userContent);
                }}
        ));
        paramMap.put("stream", false);

        // 创建请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Bearer " + aiApiProperties.getApiKey());
        headers.put("Content-Type", "application/json");
        Result result = new Result(paramMap, headers);
        return result;
    }

    private record Result(Map<String, Object> paramMap, Map<String, String> headers) {
    }


}